package com.business.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.business.system.dao.TabCarPartsMapper;
import com.business.system.pojo.TabCarParts;
import com.business.system.pojo.TabCarPartsExample;
import com.business.system.service.ITabCarPartsService;
import com.utility.Page;

@Service
public class TabCarPartsServiceImpl implements ITabCarPartsService {

	@Autowired
	private TabCarPartsMapper carPartsMapper;

	@Override
	public int insert(TabCarParts entity) throws RuntimeException {
		// TODO Auto-generated method stub
		entity.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date()));
		return this.carPartsMapper.insert(entity);
	}

	@Override
	public int update(TabCarParts entity) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.carPartsMapper.updateByPrimaryKeySelective(entity);
	}

	@Override
	public int delete(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.carPartsMapper.deleteByPrimaryKey(id);
	}

	@Override
	public TabCarParts findById(int id) throws RuntimeException {
		// TODO Auto-generated method stub
		return this.carPartsMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<TabCarParts> findList(TabCarParts entity)
			throws RuntimeException {
		// TODO Auto-generated method stub
		TabCarPartsExample example = new TabCarPartsExample();
		if (entity.getPartsIndex() != null
				&& !"".equals(entity.getPartsIndex())) {
			example.createCriteria().andPartsIndexLike(entity.getPartsIndex());
		}
		if (entity.getPartsNo() != null && !"".equals(entity.getPartsNo())) {
			example.createCriteria().andPartsNoEqualTo(entity.getPartsNo());
		}
		if (entity.getPartsName() != null && !"".equals(entity.getPartsName())) {
			example.createCriteria().andPartsNameLike(entity.getPartsName());
		}

		return this.carPartsMapper.selectByExample(example);
	}

	@Override
	public Page<TabCarParts> findListPage(TabCarParts entity, int pageNo,
			int pageSize) throws RuntimeException {
		// TODO Auto-generated method stub
		Page<TabCarParts> page = new Page<TabCarParts>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("partsNo", entity.getPartsNo());
		map.put("partsIndex", entity.getPartsIndex());
		map.put("partsName", entity.getPartsName());
		page.setParams(map);
		page.setPageNo(pageNo);
		page.setPageSize(pageSize);

		List<TabCarParts> list = this.carPartsMapper.findListPage(page);
		page.setResults(list);
		return page;
	}

}
